package com.zego.wawaji.ui.center.address;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.recycler.BaseRecyclerViewAdapter;
import com.zego.wawaji.ui.base.recycler.BaseRecyclerViewHolder;
import com.zego.wawaji.ui.center.address.bean.Address;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

/**
 * 地址管理
 * Created by Su on 2017/11/27.
 */
public class AddressAdapter extends BaseRecyclerViewAdapter<Address> implements View.OnClickListener {
    @Override
    public int[] getItemLayouts() {
        return new int[]{R.layout.my_address_item};
    }

    @Override
    public void onBindRecycleViewHolder(BaseRecyclerViewHolder viewHolder, int position) {
        View marginTop = viewHolder.findViewById(R.id.margin_top);
        View marginBottom = viewHolder.findViewById(R.id.margin_bottom);
        marginBottom.setVisibility(View.GONE);
        marginTop.setVisibility(View.GONE);
        if (position == 0) {
            marginTop.setVisibility(View.VISIBLE);
        }
        if (position == getListSize() - 1) {
            marginBottom.setVisibility(View.VISIBLE);
        }

        final Address address = getItem(position);
        TextView tvAddress = viewHolder.findViewById(R.id.address_detail);
        TextView tvName = viewHolder.findViewById(R.id.name);
        TextView tvPhone = viewHolder.findViewById(R.id.phone);
        ImageView ivDefault = viewHolder.findViewById(R.id.iv_default_addr);
        TextView tvDefault = viewHolder.findViewById(R.id.tv_default_addr);

        tvAddress.setText(address.getProvince() + address.getCity() + address.getDistrict() + address.getStreet());
        tvName.setText(address.getReceiverName());
        tvPhone.setText(address.getTelephone());

        ivDefault.setSelected(address.isDefault());
        tvDefault.setText(address.isDefault() ? "默认地址" : "设为默认");

        viewHolder.findViewById(R.id.delete).setOnClickListener(v -> ModuleMgr.getCenterMgr().reqDeleAddress(address.getId(), null));
        viewHolder.findViewById(R.id.edit).setOnClickListener(v -> UIShow.showMyEditAddressAct(AppTinkerLike.getActivity(), address));
        viewHolder.findViewById(R.id.set_default).setOnClickListener(v -> ModuleMgr.getCenterMgr().reqDefaultAddress(address.getId(), null, true));
    }

    @Override
    public int getRecycleViewItemType(int position) {
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete:       // 删除
                break;
        }
    }
}
